package com.skyworth.ui.api;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.os.Build;
import android.support.annotation.NonNull;
import android.view.View;
import android.view.animation.DecelerateInterpolator;
import android.widget.FrameLayout;

import com.skyworth.commen.ui.R;
import com.skyworth.util.SkyScreenParams;

import java.lang.ref.WeakReference;

/**
 * Description: 新loadingView<br>
 * Created by wzh on 2018/1/12 09:24.
 */
public class NewSkyLoadingView extends FrameLayout {

    private Context mContext;
    private View rectView1, rectView2, rectView3;
    private AnimatorSet mShowAnimatorSet;
    private AnimatorSet mHideAnimatorSet;
    private boolean isAnimRunning = false;
    private boolean isStop = false;
    private static final int RECT_W = 22;
    private static final int RECT_H = 22;

    public NewSkyLoadingView(@NonNull Context context) {
        super(context);
        mContext = context;
        initView();
    }

    public void destroy()
    {
        if(mShowAnimatorSet != null)
            mShowAnimatorSet.removeAllListeners();
        if(mHideAnimatorSet != null)
            mHideAnimatorSet.removeAllListeners();
        stopAnim();
    }

    private void initView() {
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(SkyScreenParams.getInstence(mContext).getResolutionValue(RECT_W),
                SkyScreenParams.getInstence(mContext).getResolutionValue(RECT_H));
        rectView3 = new View(mContext);
        rectView3.setBackgroundResource(R.drawable.ui_sdk_loading_rect);
        rectView3.setAlpha(0);
        this.addView(rectView3, params);
        params = new FrameLayout.LayoutParams(SkyScreenParams.getInstence(mContext).getResolutionValue(RECT_W),
                SkyScreenParams.getInstence(mContext).getResolutionValue(RECT_H));
        params.leftMargin = SkyScreenParams.getInstence(mContext).getResolutionValue(RECT_W + 30);
        rectView2 = new View(mContext);
        rectView2.setBackgroundResource(R.drawable.ui_sdk_loading_rect);
        rectView2.setAlpha(0);
        this.addView(rectView2, params);
        params = new FrameLayout.LayoutParams(SkyScreenParams.getInstence(mContext).getResolutionValue(RECT_W),
                SkyScreenParams.getInstence(mContext).getResolutionValue(RECT_H));
        params.leftMargin = SkyScreenParams.getInstence(mContext).getResolutionValue(110);
        rectView1 = new View(mContext);
        rectView1.setBackgroundResource(R.drawable.ui_sdk_loading_rect);
        rectView1.setAlpha(0);
        this.addView(rectView1, params);
        initAnimation();
    }

    private void initAnimation() {
        if (mShowAnimatorSet == null)
            mShowAnimatorSet = new AnimatorSet();
        if (mHideAnimatorSet == null)
            mHideAnimatorSet = new AnimatorSet();
        initShowAnimation(rectView1, 560, 0, 0, 78);
        initShowAnimation(rectView2, 480, 80, 0, 96);
        initShowAnimation(rectView3, 400, 160, 0, 108);

        initHideAnimator(rectView1, 440, 200, 78, 78 + 108);
        initHideAnimator(rectView2, 440, 280, 96, 96 + 105);
        initHideAnimator(rectView3, 360, 440, 108, 108 + 100);
    }

    private void initShowAnimation(final View view, long duration, long startDelay, int startX, int endX) {
        ObjectAnimator translationAnimator = ObjectAnimator.ofFloat(view, "translationX",
                SkyScreenParams.getInstence(mContext).getResolutionValue(startX),
                SkyScreenParams.getInstence(mContext).getResolutionValue(endX));
        translationAnimator.setDuration(duration);
        translationAnimator.setInterpolator(new DecelerateInterpolator());
        translationAnimator.setStartDelay(startDelay);

        ObjectAnimator alphaAnimator = ObjectAnimator.ofFloat(view, "alpha", 0.0f, 1.0f);
        alphaAnimator.setDuration(duration);

        mShowAnimatorSet.playTogether(translationAnimator, alphaAnimator);
        mShowAnimatorSet.addListener(new ShowAnimatorListenerAdapter(this, view));
    }

    static class ShowAnimatorListenerAdapter extends AnimatorListenerAdapter
    {
        WeakReference<NewSkyLoadingView> loadingRef;
        WeakReference<View> viewRef;

        public ShowAnimatorListenerAdapter(NewSkyLoadingView loadingView, View v)
        {
            loadingRef = new WeakReference<NewSkyLoadingView>(loadingView);
            viewRef = new WeakReference<View>( v);
        }

        @Override
        public void onAnimationEnd(Animator animation) {
            super.onAnimationEnd(animation);
            if(loadingRef == null)
                return ;
            NewSkyLoadingView loadingView = loadingRef.get();
            if(loadingView == null)
                return ;
            if (loadingView.mHideAnimatorSet != null) {
                if (!loadingView.isStop)
                    loadingView.mHideAnimatorSet.start();
            }
        }

        @Override
        public void onAnimationCancel(Animator animation) {
            super.onAnimationCancel(animation);
            if(viewRef == null)
                return ;
            View v = viewRef.get();
            if(v == null)
                return ;
            v.setTranslationX(0);
            v.setAlpha(0f);
        }
    }

    private void initHideAnimator(final View view, long duration, long startDelay, int startX, int endX) {
        ObjectAnimator translationAnimator = ObjectAnimator.ofFloat(view, "translationX",
                SkyScreenParams.getInstence(mContext).getResolutionValue(startX),
                SkyScreenParams.getInstence(mContext).getResolutionValue(endX));
        translationAnimator.setDuration(duration);
        translationAnimator.setStartDelay(startDelay);

        ObjectAnimator alphaAnimator = ObjectAnimator.ofFloat(view, "alpha", 1.0f, 0.0f);
        if (Build.VERSION.SDK_INT >= 26)
            alphaAnimator.setStartDelay(startDelay);
        alphaAnimator.setDuration(duration);

        mHideAnimatorSet.playTogether(translationAnimator, alphaAnimator);
        mHideAnimatorSet.addListener(new HideAnimatorListenerAdapter(this, view));
    }

    static class HideAnimatorListenerAdapter extends AnimatorListenerAdapter
    {
        WeakReference<NewSkyLoadingView> loadingRef;
        WeakReference<View> viewRef;

        public HideAnimatorListenerAdapter(NewSkyLoadingView loadingView, View v)
        {
            loadingRef = new WeakReference<NewSkyLoadingView>(loadingView);
            viewRef = new WeakReference<View>( v);
        }

        @Override
        public void onAnimationEnd(Animator animation) {
            super.onAnimationEnd(animation);

            if(loadingRef != null)
            {
                NewSkyLoadingView loadingView = loadingRef.get();
                if(loadingView != null)
                {
                    if (loadingView.mShowAnimatorSet != null) {
                        if (!loadingView.isStop)
                            loadingView.mShowAnimatorSet.start();
                    }
                }
            }

            if(viewRef == null)
                return ;
            View view = viewRef.get();
            if(view == null)
                return ;
            view.setTranslationX(0);
            view.setAlpha(0f);
        }

        @Override
        public void onAnimationCancel(Animator animation) {
            super.onAnimationCancel(animation);
            if(viewRef == null)
                return ;
            View v = viewRef.get();
            if(v == null)
                return ;
            v.setTranslationX(0);
            v.setAlpha(0f);
        }
    }

    @Override
    protected void onVisibilityChanged(@NonNull View changedView, int visibility) {
        super.onVisibilityChanged(changedView, visibility);
        if (visibility == VISIBLE)
            startAnim();
        else
            stopAnim();
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        stopAnim();
        mShowAnimatorSet = null;
        mHideAnimatorSet = null;
    }

    public boolean isAnimRunning() {
        return isAnimRunning;
    }

    public synchronized void startAnim() {
        isStop = false;
        if (isAnimRunning()) return;
        isAnimRunning = true;
//        this.setLayerType(View.LAYER_TYPE_HARDWARE, null);
        if (mShowAnimatorSet == null)
            initAnimation();
        mShowAnimatorSet.start();
        invalidate();
    }

    public synchronized void stopAnim() {
        isStop = true;
        if (!isAnimRunning()) return;
        isAnimRunning = false;
//        this.setLayerType(View.LAYER_TYPE_NONE, null);
        if (mShowAnimatorSet != null) {
            mShowAnimatorSet.cancel();
        }
        if (mHideAnimatorSet != null) {
            mHideAnimatorSet.cancel();
        }
        invalidate();
    }

}
